/*
 * @Author: sympsel sympsel@outlook.com
 * @Date: 2025-09-02 13:27:02
 * @LastEditors: sympsel sympsel@outlook.com
 * @LastEditTime: 2025-09-02 20:55:34
 * @FilePath: \work25_9_2\src\test.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * 过河卒问题
 */

#include <iostream>

void testPrint(int** arr, int m, int n) {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << "\n";
    }
    std::cout << "\n";
}

int main() {
    int x = 0, y = 0, m = 0, n = 0;
    int flag = 1;

    while (flag) {
        std::cout << "input dest position:>";
        std::cin >> m >> n;
        if (m > 0 && n > 0) {
            flag = 0;
        }
    }

    int** arr = new int*[m];
    for (int i = 0; i < m; i++) {
        arr[i] = new int[n];
    }

    flag = 1;
    while (flag) {
        std::cout << "input house position:>";
        std::cin >> x >> y;
        if (x > 0 && y > 0 && x <= m && y <= n && !(x == m && y == n)) {
            flag = 0;
        }
    }
    x--;
    y--;

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            arr[i][j] = 1;
        }
    }

    arr[x][y] = 0;

    int aX[] = {-2, -1, 1, 2, 2, 1, -1, -2};
    int aY[] = {-1, -2, -2, -1, 1, 2, 2, 1};
    for (int i = 0; i < 8; i++) {
        int newX = x + aX[i];
        int newY = y + aY[i];
        if (newX >= 0 && newX < m && newY >= 0 && newY < n) {
            arr[newX][newY] = 0;
        }
    }

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
             if (i == 0 && j == 0) continue;
            if (arr[i][j] == 0) {
                continue;
            }
            if (i == 0) {
                arr[i][j] = arr[i][j - 1];
            } else if (j == 0) {
                arr[i][j] = arr[i - 1][j];
            } else {
                arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
            }
        }
    }

    testPrint(arr, m, n);
    std::cout << "result: " << arr[m - 1][n - 1];

    for (int i = 0; i < m; i++) {
        delete[] arr[i];
    }
    delete[] arr;

    return 0;
}